package com.fbli.upload.Utils;

import java.io.*;

public class StreamUtils {
    /**
     * 将InputStream输入流转换成byte[],即可以把文件内容读入到byte[]
     * @param is
     * @return
     * @throws IOException
     */
    public static byte[] streamToByteArray(InputStream is) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();//创建输出流对象
        byte[] b = new byte[1024];
        int len;
        while ((len=is.read(b))!=-1){
            bos.write(b,0,len);
        }
        byte[] array = bos.toByteArray();
        bos.close();
        return array;
    }

    /**
     * 将InputStream转换成String
     * @param is
     * @return
     * @throws IOException
     */
    public static String streamToString(InputStream is) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
        StringBuilder builder=new StringBuilder();
        String line;
        while ((line=bufferedReader.readLine())!=null){
            builder.append(line+"\r\n");
        }
        return builder.toString();
    }
}

